package cn.stylefeng.guns.modular.highlight.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.erlist.entity.ErList;
import cn.stylefeng.guns.modular.highlight.entity.Highlight;
import cn.stylefeng.guns.modular.highlight.mapper.HighlightMapper;
import cn.stylefeng.guns.modular.highlight.model.param.HighlightParam;
import cn.stylefeng.guns.modular.highlight.service.HighlightService;
import cn.stylefeng.guns.modular.highlight.enums.HighlightExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 光数据 服务实现类
 *
 * @author raindeer
 * @date 2021/05/24 14:50
 */
@Service
public class HighlightServiceImpl extends ServiceImpl<HighlightMapper, Highlight> implements HighlightService {

    @Override
    public PageResult<Highlight> page(HighlightParam highlightParam) {

        // 构造条件
        LambdaQueryWrapper<Highlight> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(highlightParam)) {
            if (ObjectUtil.isNotNull(highlightParam.getTitle())) {
                queryWrapper.like(Highlight::getTitle,highlightParam.getTitle());
            }
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Highlight> list(HighlightParam highlightParam) {

        // 构造条件
        LambdaQueryWrapper<Highlight> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(HighlightParam highlightParam) {

        // 将dto转为实体
        Highlight highlight = new Highlight();
        BeanUtil.copyProperties(highlightParam, highlight);

        this.save(highlight);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(HighlightParam highlightParam) {
        this.removeById(highlightParam.getId());
    }

    @Override
    public void edit(HighlightParam highlightParam) {

        // 根据id查询实体
        Highlight highlight = this.queryHighlight(highlightParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(highlightParam, highlight);

        this.updateById(highlight);
    }

    @Override
    public Highlight detail(HighlightParam highlightParam) {
        return this.queryHighlight(highlightParam);
    }



    /**
     * 获取光数据
     *
     * @author raindeer
     * @since 2021/05/24 14:50
     */
    private Highlight queryHighlight(HighlightParam highlightParam) {
        Highlight highlight = this.getById(highlightParam.getId());
        if (ObjectUtil.isEmpty(highlight)) {
            throw new ServiceException(HighlightExceptionEnum.NOT_EXISTED);
        }
        return highlight;
    }
}